package com.goldgov.information.service.impl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.goldgov.information.dao.ArticleDao;
import com.goldgov.information.service.Article;
import com.goldgov.information.service.ArticleQuery;
import com.goldgov.information.service.ArticleService;
import com.goldgov.utils.DateUtil;
import com.goldgov.utils.PropertyUtil;
import com.goldgov.utils.UrlUtil;

@Service
public class ArticleServiceImpl implements ArticleService {

	@Autowired
	ArticleDao articleDao;
	
	protected final Log logger = LogFactory.getLog(this.getClass());
	
	@Override
	public List<Article> getArticleList(ArticleQuery query) {
		return articleDao.getArticleList(query);
	}

	@Override
	public void updateArticle(Article article) {
		articleDao.updateArticle(article);
	}
	
	@Override
	public void publishArticles(String[] ids) {
		if(null!=ids && ids.length>0) {
			for(int i=0;i<ids.length;i++) {
				Article obj=articleDao.getArticleByID(ids[i]);
				publishArticle(obj);
			}
		}
	}
	
	@Override
	public void publishArticle(Article obj) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		obj.setPublishState(Article.ACTIVE_STATE_YES);
		obj.setPublishDate(new Date());
		articleDao.updateArticle(obj);
		Map<String,String> params=new HashMap<>();
		if(null!=obj.getArticleAuthor() && !"".equals(obj.getArticleAuthor())) {
			params.put("articleAuthor", obj.getArticleAuthor());
		}
		if(null!=obj.getArticleContent() && !"".equals(obj.getArticleContent())) {
			params.put("articleContent", obj.getArticleContent());
		}
		if(null!=obj.getArticleSource() && !"".equals(obj.getArticleSource())) {
			params.put("articleSource", obj.getArticleSource());
			//当为爬取数据时，创建人为数据来源
			params.put("basicPeopleName", obj.getArticleSource());
		}
		if(null!=obj.getArticleSort()) {
			params.put("basicSort", obj.getArticleSort().toString());
		}
		if(null!=obj.getArticleKeyWord() && !"".equals(obj.getArticleKeyWord())) {
			params.put("articleKeyword", obj.getArticleKeyWord());
		}
		if(null!=obj.getArticleTitle() && !"".equals(obj.getArticleTitle())) {
			params.put("basicTitle", obj.getArticleTitle());
		}
		if(null!=obj.getPublishDateText() && !"".equals(obj.getPublishDateText())) {
			params.put("articlePublishDateText", obj.getPublishDateText());
		}
		if(null!=obj.getArticleUrl() && !"".equals(obj.getArticleUrl())) {
			params.put("articleSite", obj.getArticleUrl());
		}
		params.put("basicDateTime",obj.getPublishDate()==null?sdf.format(new Date()):sdf.format(obj.getPublishDate()));
		params.put("basicCategoryId", obj.getCmsCategoryID()==null?"154":obj.getCmsCategoryID().toString());
		params.put("articleStick", obj.getStickState()==null?"2":obj.getStickState().toString());
		params.put("articleAttribute", obj.getArticleAttribute().toString());
		params.put("articleStatus", Article.PUBLISH_STATE_FABU.toString());
		params.put("isGrab", Article.ARTICLE_IS_GRAB.toString());
		Connection conn = Jsoup.connect(UrlUtil.CMS_ARTICLE_URL).ignoreContentType(true);
		conn.data(params);
		try {
			conn.post();
		} catch (IOException e) {
			logger.error("文章发布到cms失败",e);
			logger.info("参数们=========="+params);
		}
	}

	@Override
	public void addArticle(Article article) {
		if (PropertyUtil.objectNotEmpty(article.getPublishDateText())) {
			article.setPublishDate(DateUtil.getTime(article.getPublishDateText()));
		}
		articleDao.addArticle(article);
	}

	@Override
	public Article getArticleByID(String articleID) {
		return articleDao.getArticleByID(articleID);
	}

	@Override
	public int existArticleByUrl(String url) {
		return articleDao.existArticleByUrl(url);
	}

	@Override
	public void deleteArticleByIDs(String[] ids) {
		articleDao.deleteArticleByIDs(ids);
	}

	@Override
	public void cancelArticles(String[] ids) {
		articleDao.cancelArticles(ids);
	}

	@Override
	public void deleteArticlesByRulesID(String id) {
		articleDao.deleteArticlesByRulesID(id);
	}

	@Override
	public List<String> statisticsBQByArticle() {
		return articleDao.statisticsBQByArticle();
	}

	@Override
	public List<String> statisticsBQByUser() {
		return articleDao.statisticsBQByUser();
	}
}
